#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
修复数据文件中缺失的字段，确保所有记录都包含专业和班级字段
"""

import json
import os

def fix_missing_fields():
    """修复数据文件中缺失的字段"""
    data_file = "paper_titles_unified.json"
    
    if not os.path.exists(data_file):
        print(f"数据文件 {data_file} 不存在")
        return False
    
    try:
        # 读取现有数据
        with open(data_file, 'r', encoding='utf-8') as f:
            data = json.load(f)
        
        print(f"读取到 {len(data)} 条记录")
        
        # 为每条记录检查并添加缺失的字段
        updated_count = 0
        for item in data:
            # 确保所有必要的字段都存在
            if "major" not in item:
                item["major"] = ""
                updated_count += 1
                
            if "class_name" not in item:
                item["class_name"] = ""
                updated_count += 1
                
            # 确保其他必要字段也存在
            if "title" not in item:
                item["title"] = ""
                
            if "teacher" not in item:
                item["teacher"] = ""
                
            if "student" not in item:
                item["student"] = ""
                
            if "student_id" not in item:
                item["student_id"] = ""
                
            if "create_time" not in item:
                item["create_time"] = ""
        
        # 保存更新后的数据
        with open(data_file, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        
        print(f"成功更新 {updated_count} 个缺失字段")
        return True
        
    except Exception as e:
        print(f"修复数据时出错: {e}")
        return False

if __name__ == "__main__":
    fix_missing_fields()